<template>
	<view class="wrap">
		<view class="screen" @click="onJumpHome" v-if="screenImage" :style="{ height: screenHeight + 'px' }">
			<image :src="screenImage" mode="aspectFill" :style="{ height: screenHeight + 'px' }"></image>
			<view class="b-countdown">
				<view class="s-text">{{screenCountdown}}</view>
			</view>
		</view>
		<!-- 加载中 -->
		<load-more :loadingType="loadingType" :loadingText="loadingText" :top="300"></load-more>
	</view>
</template>

<script>
	import {
		getConfig
	} from '@/service/api/common.js'
	import {
		silentReload,
		smartLogin
	} from '@/service/request/main.js'
	import Util from '../../common/util.js'
	import mixinsCommon from '../../mixins/common.js'

	export default {
		mixins: [mixinsCommon],
		data() {
			return {
				from: '',
				screenHeight: 0,
				screenImage: '',
				screenCountdown: 5,
				loadingType: 1,
				loadingText: '',
				interval: 0
			}
		},
		onLoad(options) {
			console.log('web onLoad', options)
			// 设置场景
			this.$store.commit('setScenario', 'activity')

			let params = options
			this.from = params.from
			if (params.scene) {
				params = Util.urlDecode(decodeURIComponent(params.scene))
				this.from = 'qrcode'
			}
			this.$store.commit('setActivityId', params.aid)
			getConfig().then(([err, res]) => {
				console.log('getConfig', err, res);
				if (!err) {
					this.loadingType = -1
					this.$store.commit('setGlobalConfig', res)
					this.screenImage = res.welcome_img
					this.screenCountdown = res.welcome_countdown || 5
					if (this.screenImage) {
						// 验证码倒计时
						this.interval = setInterval(() => {
							this.screenCountdown--
						}, 1000)
						setTimeout(() => {
							if (this.interval > 0) {
								this.onJumpHome()
							}
						}, this.screenCountdown * 1000)
					} else {
						this.onJumpHome()
					}
				} else {
					this.loadingType = 3
					this.loadingText = err.data.msg || '加载失败'
				}
			})

			// 获取窗口高度
			const systemInfo = uni.getSystemInfoSync()
			this.screenHeight = systemInfo.screenHeight
		},
		methods: {
			onJumpHome() {
				console.log('onJumpHome')
				if (this.interval > 0) {
					clearInterval(this.interval)
					this.interval = 0
				}
				if (this.$store.getters.accessToken) {
					console.log('hasLogin')
					uni.switchTab({
						url: '/pages/home/index' // 跳到首页
					})
				} else {
					if (this.from === 'qrcode') {
						console.info('needLogin qrcode', 'silentReload')
						silentReload(false, '') // 扫描尝试自动登录
					} else {
						console.log('needLogin jump')
						smartLogin()
					}
				}
			}
		}
	}
</script>

<style lang="scss">
	.wrap {}

	.screen {
		width: 750upx;

		image {
			width: 750upx;

		}

		.b-countdown {
			position: absolute;
			right: 80upx;
			bottom: 80upx;
			width: 80upx;
			height: 80upx;
			border-radius: 40upx;
			background: rgba(0, 0, 0, 0.5);
			display: flex;
			align-items: center;
			justify-content: center;

			.s-text {
				font-size: 32upx;
				font-weight: bold;
				color: #fff;
			}
		}
	}
</style>
